package cn.ucox.web.ms.controller;

import cn.ucox.web.ms.Constant;
import cn.ucox.web.ms.components.cache.TableMetaInfoCache;
import cn.ucox.web.ms.domain.TableColumn;
import cn.ucox.web.ms.service.ITriggerService;
import cn.ucox.web.ms.view.ApiResult;
import com.dragonsoft.adapter.service.QueryAdapterSend;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by Administrator on 2017/9/7.
 */
@Controller
@RequestMapping(value = "trigger")
public class TriggerController {

    @Resource
    private ITriggerService triggerService;

    @ResponseBody
    @RequestMapping(value = "tables", method = GET)
    public ApiResult gotoCreatePage(HttpServletRequest request, Model model) {

        String database = request.getParameter("database");
        String schemaname = request.getParameter("schemaname")==null?"public":request.getParameter("schemaname");

        List<String> list = triggerService.getTableList(database,schemaname);

        return new ApiResult<List<String>>().success("数据表表信息",list);
    }

    @ResponseBody
    @RequestMapping(value = "column", method = POST)
    public ApiResult loadColumnList(HttpServletRequest request) {

        String tableName = request.getParameter("tableName");
        String schemaname = request.getParameter("schemaname")==null?"public":request.getParameter("schemaname");

        if(StringUtils.isNotBlank(tableName)){
            List<TableColumn> columnList = triggerService.getColumnDatas(tableName,schemaname);
            return new ApiResult<List<TableColumn>>().success("表字段信息",columnList);
        }

        return new ApiResult().failure("表字段信息获取失败");
    }

    @ResponseBody
    @RequestMapping(value = "create", method = POST)
    public ApiResult createTrigger(HttpServletRequest request) {

        String tableName = request.getParameter("tableName");
        String att = request.getParameter("att")==null?"0":request.getParameter("att");

        String[] columnList = request.getParameterValues("columnList");

        if(null == columnList || 0 == columnList.length){
            return new ApiResult().failure("请至少选择一个列表字段");
        }

        String[] triggerTypes = request.getParameterValues("triggerTypes");
        if(null == triggerTypes || 0 == triggerTypes.length){
            return new ApiResult().failure("请至少选择一个待生成触发器类型");
        }

        String level = request.getParameter("level")==null? Constant.SYNC_LEVEL_HIGH:request.getParameter("level");

        return triggerService.createTrigger(tableName,att,columnList,triggerTypes,level);

    }

}
